package com.icss.daos;
/*
 * 论坛二级版块dao类
 * 管志春
 * 2010.03.31
 * 
 * */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.icss.beans.BoardVO;

import com.icss.utils.DB;

public class BoardDao {

	private Statement stat = null;

	private Connection conn = null;

	private ResultSet rs = null;
	
	//获取所有版块信息
	public ArrayList<BoardVO> getAllBoard() {

		ArrayList<BoardVO> boardvos = new ArrayList<BoardVO>();
		conn = DB.getConnection();
		String sql = "select * from Board";
		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);

			while (rs.next()) {
				BoardVO boradVO = new BoardVO();
				boradVO.setBoardID(rs.getInt("boardID"));
				boradVO.setBoardName(rs.getString("boardName"));
				boradVO.setBoardMsg(rs.getString("boardMsg"));

				boardvos.add(boradVO);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return boardvos;
	}
	
	//根据classID获取版块
	public ArrayList<BoardVO> getBoardByClassID(int id) {
		ArrayList<BoardVO> boardvos = new ArrayList<BoardVO>();
		conn = DB.getConnection();
		String sql = "select * from Board where classID=" + id;
		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);

			while (rs.next()) {
				BoardVO boradVO = new BoardVO();
				boradVO.setBoardID(rs.getInt("boardID"));
				boradVO.setBoardName(rs.getString("boardName"));
				boradVO.setBoardMsg(rs.getString("boardMsg"));

				boardvos.add(boradVO);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		return boardvos;
	}
	
	//根据ID获取版块信息
	public BoardVO getBoardByID(int id) {

		BoardVO boardvo = new BoardVO();
		conn = DB.getConnection();
		String queryStr = "select * from sale where id =" + id;

		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(queryStr);

			while (rs.next()) {
				boardvo.setBoardID(rs.getInt("boardID"));
				boardvo.setBoardName(rs.getString("boardName"));
				boardvo.setBoardMsg(rs.getString("boardMsg"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close();
		return boardvo;

	}
	
	//根据名称获取版块信息
	public BoardVO getBoardByName(String name) {

		BoardVO boardvo = new BoardVO();
		conn = DB.getConnection();
		String queryStr = "select * from Board where boardName = '" + name + "'";
		System.out.println(queryStr);
		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(queryStr);

			while (rs.next()) {
				boardvo.setBoardID(rs.getInt("boardID"));
				boardvo.setBoardName(rs.getString("boardName"));
				boardvo.setBoardMsg(rs.getString("boardMsg"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close();
		return boardvo;

	}
	
	//执行SQL语句
	public boolean executeSQL(String sql) {

		int result = 0;

		try {

			conn = DB.getConnection();

			stat = conn.createStatement();

			result = stat.executeUpdate(sql);

		} catch (SQLException e) {

			close();
			e.printStackTrace();
		}

		close();
		if (result != 0) {
			return true;
		}
		return false;

	}

	public void close() {

		try {

			if (null != conn) {
				conn.close();
			}

			if (null != stat) {
				stat.close();
			}

			if (null != rs) {
				rs.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();

		}
	}
}
